APM.BusinessDetailExperiencePageController = Ember.Controller.extend({
	needs: ["businessCenter", "businessDetail"],

	business: Ember.computed.alias('controllers.businessDetail.model'),

	clickTimePoint: Ember.computed.oneWay('controllers.businessDetail.clickTimePoint'),

	timePointLisenterEvent:Ember.computed("clickTimePoint", function(key, value){
		var view = Broada.getView("businessTimeRangesTopo");
		if(this.get("clickTimePoint") != null){
			var date = new Date(this.get("clickTimePoint"));
			if(typeof view != "undefined"){
				var range = view.getTimeRange().endDate - view.getTimeRange().startDate;				
				this.set("startDate", new Date(date-range));
				this.set("endDate", date);
			}
			typeof view != "undefined"?view.set("btnTxt", date?APM.util.formatDate(date, "f"):null):null;
			this.set("dayTime",APM.util.formatDate(date, "yyyy-MM-dd"));
			this.requirePerformances();
		}
	}),

	dayTime:APM.util.formatDate(new Date(new Date() - timeD_value),"yyyy-MM-dd"),

	dataKeyRange:24,

	businessChange: Ember.observer("business", function(){
		this.set("performances", null);
		this.set("uri", null);
		this.set("detailPerformances", null);
	}),

	performances : Ember.A(),

	detailPerformances : Ember.A(),

	defaultSelect : "system/response_time/avg",

	init: function(){
		
		this.set("cancelSet",true);


		if(overviewOrCenterSwitch == "business"){
			this.set("defaultSelect","business/response_time/avg");
		}else{
			this.set("defaultSelect","system/response_time/avg");
		}	
	},

	requirePerformances: function(time){
		var ctrl = this;
		var business = this.get("business");



		var sortField = this.get("defaultSelect");

		var startDate = this.get("startDate"),
			endDate = this.get("endDate");

		var todySDate = new Date(APM.util.formatDate(startDate,"yyyy-MM-dd"));
		var todyEDate = new Date(APM.util.formatDate(endDate,"yyyy-MM-dd"));
			startDate = new Date(todySDate.setHours(0));
			todyEDate.setHours(23);
			todyEDate.setMinutes(59);
			todyEDate.setSeconds(59);
			endDate = new Date(todyEDate);

		var params = {
			appId: business.id,
			sortField: sortField,
			top: 20,
			startDate: +startDate,
			endDate: +endDate
		}
		//开发调试模式参数
		if( isLocalDebugModel ){
			delete params.sortField;
			delete params.top;
			delete params.startDate;
			delete params.endDate;
		}

		return this.store.ajax("GET", "performances/page", params)
		.then(function(response){
			ctrl.set("performances", Ember.A(response.result));
			if(response.result!=undefined && response.result.length > 0) {
				var resourceId = response.result[0].resourceId;
				ctrl.set("uri",ctrl.newLine(response.result[0].uri));	
			}
			return response;
		});
	},

	requireDetailPerformances: function(time, resourceId){
		var ctrl = this;
		var business = this.get("business");

		sortField = this.get("defaultSelect");

		var startDate = this.get("startDate"),
			endDate = this.get("endDate");
			
		var todySDate = new Date(APM.util.formatDate(startDate,"yyyy-MM-dd"));
		var todyEDate = new Date(APM.util.formatDate(endDate,"yyyy-MM-dd"));
			startDate = new Date(todySDate.setHours(0));
			todyEDate.setHours(23);
			todyEDate.setMinutes(59);
			todyEDate.setSeconds(59);
			endDate = new Date(todyEDate);

		var params = {
			appId: business.id,
			field: sortField,
			startDate: +startDate,
			endDate: +endDate,
			size: "minutes"
		}
		//开发调试模式参数
		if( isLocalDebugModel ){
			endDate = new Date(2014, 9, 1, 12, 0, 0);
			startDate = new Date(+endDate)
			startDate.setHours(startDate.getHours()-1);
			delete params.sortField;
			delete params.startDate;
			delete params.endDate;
		}

		return this.store.ajax("GET", "performances/page/"+resourceId, params)
		.then(function(response){
			
			var performances = ctrl.get("controllers.businessCenter")
				.performancesCompletion(response.result, startDate, endDate,undefined,true);
			ctrl.set("detailPerformances", Ember.A(performances));
			return response;
		});
	},


	newLine : function (str) {
		if(str.indexOf("http:") == 0) {
			this.set("isHttp", true);
		}else {
			this.set("isHttp", false);
		}
		var i = 0;
		// var newStr = "";
		// for(; i < parseInt(str.length/50); i++) {
		// 	newStr=newStr + str.substring(0+i*50,(i+1)*50)+"\n";
		// }
		// newStr = newStr + str.substring(i*50);
		return str;
	},

	actions : {
		selectOnChange : function(data, view) {
			this.set("defaultSelect", data);
			this.requirePerformances();
		},

		itemClick : function(data, view) {
			$(view).addClass('active').siblings().removeClass('active');
			this.set("uri", this.newLine(data.uri));
			this.requireDetailPerformances(null, data.resourceId);
		},

		timeRangesChange: function(timeRange){
			this.set("startDate", timeRange.startDate);
			this.set("endDate", timeRange.endDate);
			this.requirePerformances();
		}
	}
});

	